package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.LoginAnnotation;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.IRedisService;
import cn.wolfcode.wolf2w.redis.IRedisStatisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Value("${sms.url}")
    private String url;
    @Value("${sms.appkey}")
    private String appkey;
    @Autowired
    private IRedisService redisService ;
    @Autowired
    private IRedisStatisService statisService;
    @Autowired
    private IUserInfoService userInfoService;
    @GetMapping("/get")
    public Object get(String id){
        UserInfo userInfo = userInfoService.get(id);
        System.out.println(userInfo);
        return userInfo;
    }

    /**
     * 判断号码是否已注册
     * @param phone
     * @return
     */
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        Boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    /**
     * 根据手机号码发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone,String url,String appkey){
        userInfoService.sendVerifyCode(phone,url,appkey);
        return JsonResult.success();
    }
    /**
     * 根据手机号码发送验证码
     * @param phone
     * @return
     */
    @PostMapping("/regist")
    public Object regist(String phone,String nickname ,String password ,String rpassword ,String verifyCode){
        userInfoService.regist(phone,nickname,password,rpassword,verifyCode);
        return JsonResult.success();
    }

    /**
     * 登录操作
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Object login(String username,String password){
        UserInfo user = userInfoService.login(username,password);
        //登录成功后设置token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisService.setToken(token, JSON.toJSONString(user));
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        return JsonResult.success(map);
    }

    /**
     * 再次发送请求
     * @param request
     * @return
     */
    @LoginAnnotation
    @PostMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        String token = request.getHeader("token");
        UserInfo user = redisService.checkVal(token);
        if(user == null){
            return JsonResult.noLogin();
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        return JsonResult.success(map);
    }


    @GetMapping("/strategies/favor")
    public Object strategiesFavor(String userId){
        List<String> sids = statisService.strategiesFavor(userId);
        return JsonResult.success(sids);
    }
}
